package com.cxs.commoneventtest.slice;

import com.cxs.commoneventtest.ResourceTable;
import com.cxs.commoneventtest.event.MyCommonEventSubscriber;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.event.commonevent.*;
import ohos.rpc.RemoteException;



public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);


        findComponentById(ResourceTable.Id_btn_publish_event).setClickedListener(c -> publishEvent());
        findComponentById(ResourceTable.Id_btn_subscribe).setClickedListener(c -> subscribeEvent());
        findComponentById(ResourceTable.Id_btn_publish_permission_event).setClickedListener(c -> publishPermissionEvent());
        findComponentById(ResourceTable.Id_btn_publish_order_event).setClickedListener(c -> publishOrderEvent());
        findComponentById(ResourceTable.Id_btn_subscribe_order_event).setClickedListener(c -> subscribeOrderEvent());
        findComponentById(ResourceTable.Id_btn_publish_sticky_event).setClickedListener(c -> publishStickyEvent());

    }


    private void publishStickyEvent() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("com.cxs.commoneventtest.event")
                .build();
        intent.setOperation(operation);
        intent.setParam("key1", "value1");
        CommonEventData commonEventData = new CommonEventData(intent);

        CommonEventPublishInfo publishInfo = new CommonEventPublishInfo();
        publishInfo.setSticky(true);  // 设置属性为粘性公共事件

        try {
            CommonEventManager.publishCommonEvent(commonEventData, publishInfo);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void subscribeOrderEvent() {
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent("com.cxs.commoneventtest.event");

        CommonEventSubscribeInfo subscribeInfo1 = new CommonEventSubscribeInfo(matchingSkills);
        CommonEventSubscribeInfo subscribeInfo2 = new CommonEventSubscribeInfo(matchingSkills);

        subscribeInfo1.setPriority(0);
        subscribeInfo2.setPriority(1);

        CommonEventSubscriber commonEventSubscriber1 = new CommonEventSubscriber(subscribeInfo1) {
            @Override
            public void onReceiveEvent(CommonEventData commonEventData) {
                System.out.println("不耗时的任务开始111111........");
            }
        };

        CommonEventSubscriber commonEventSubscriber2 = new CommonEventSubscriber(subscribeInfo2) {
            @Override
            public void onReceiveEvent(CommonEventData commonEventData) {
                System.out.println("不耗时的任务开始22222........");
            }
        };

        try {
            CommonEventManager.subscribeCommonEvent(commonEventSubscriber1);
            CommonEventManager.subscribeCommonEvent(commonEventSubscriber2);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void publishOrderEvent() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("com.cxs.commoneventtest.event")
                .build();
        intent.setOperation(operation);
        intent.setParam("key1", "value1");

        CommonEventData commonEventData = new CommonEventData(intent);
        commonEventData.setData("你好！鸿蒙");

        CommonEventPublishInfo publishInfo = new CommonEventPublishInfo();
        publishInfo.setOrdered(true);

        try {
            CommonEventManager.publishCommonEvent(commonEventData, publishInfo);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void publishPermissionEvent() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("com.cxs.commoneventtest.event")
                .build();
        intent.setOperation(operation);

        intent.setParam("key1", "value1");

        CommonEventData commonEventData = new CommonEventData(intent);
        CommonEventPublishInfo publishInfo = new CommonEventPublishInfo();
        String[] permission = {"com.cxs.commoneventtest.permission"};
        publishInfo.setSubscriberPermissions(permission);
        try {
            CommonEventManager.publishCommonEvent(commonEventData, publishInfo);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void subscribeEvent() {
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent("com.cxs.commoneventtest.event");
        CommonEventSubscribeInfo subscribe = new CommonEventSubscribeInfo(matchingSkills);
        try {
            CommonEventManager.subscribeCommonEvent(new MyCommonEventSubscriber(subscribe));
            System.out.println("订阅事件结束了");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void publishEvent() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("com.cxs.commoneventtest.event")
                .build();
        intent.setOperation(operation);

        intent.setParam("key1", "value1");

        CommonEventData commonEventData = new CommonEventData(intent);
        try {
            CommonEventManager.publishCommonEvent(commonEventData);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
